package com.model;

import javax.servlet.http.*;

import org.apache.struts.action.*;

public class RegistrationForm extends ActionForm
{
	String username, email, password;
	
	private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	
	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username) 
	{
		this.username = username;
	}

	public String getEmail() 
	{
		return email;
	}

	public void setEmail(String email) 
	{
		this.email = email;
	}

	public String getPassword() 
	{
		return password;
	}

	public void setPassword(String password) 
	{
		this.password = password;
	}
	
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
	{
		ActionErrors actionErrors = new ActionErrors();
		
		if (username == null || username.trim().equals(""))
		{
			actionErrors.add("username", new ActionMessage("error.username"));
		}
		if (email == null || email.trim().equals(""))
		{
			actionErrors.add("email", new ActionMessage("error.email"));
		}
		else
		{
			if (!email.matches(EMAIL_PATTERN))
			{
				actionErrors.add("email", new ActionMessage("error.email.pattern"));
			}
		}
		if (password == null || password.trim().equals(""))
		{
			actionErrors.add("password", new ActionMessage("error.password"));
		}
		
		return actionErrors;
	}
	
}
